﻿using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using ABPMPATest.PhoneBooks.Dtos;
using ABPMPATest.PhoneBooks.Persons;
using Abp.UI;

namespace ABPMPATest.PhoneBooks
{
    public class PersonAppService : ABPMPATestAppServiceBase, IPersonAppService
    {
        private readonly IRepository<Person> _personRepository;
        private IPersonAppService _personAppServiceImplementation;

        public PersonAppService(IRepository<Person> personRepository)
        {
            _personRepository = personRepository;
        }

        #region 操作


        public async Task<GetPersonForEditOutput> GetPersonForPersonEditAsync(NullableIdDto input)
        {
            var output = new GetPersonForEditOutput();
            PersonEditDto personEditDto;
            if (input.Id.HasValue)//编辑
            {
                var entity = await _personRepository.GetAsync(input.Id.Value);
                personEditDto = entity.MapTo<PersonEditDto>();
            }
            else//新建
            {
                personEditDto = new PersonEditDto();
            }

            output.Person = personEditDto;
            return output;
        }

        /// <summary>
        /// 创建或更新Person实体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdatePersonAsync(CreateOrUpdatePersonInput input)
        {
            if (input.PersonEditDto.Id.HasValue)
            {
                await UpdatePersonAsync(input.PersonEditDto);
            }
            else
            {
                await CreatePersonAsync(input.PersonEditDto);
            }
        }

        /// <summary>
        /// 获取联系人相关信息，支持分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PersonListDto>> GetPagedPersonAsync(GetPersonInput input)
        {
            var query = _personRepository.GetAll();

            var personCount = await query.CountAsync();
            var persons = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            var dtos = persons.MapTo<List<PersonListDto>>();

            return new PagedResultDto<PersonListDto>(personCount, dtos);
        }

        /// <summary>
        /// 根据Id查询相关联系人信息
        /// </summary>
        /// <returns></returns>
        public async Task<PersonListDto> GetPersonByIdAsync(NullableIdDto input)
        {
            var person = await _personRepository.GetAsync(input.Id.Value);
            return person.MapTo<PersonListDto>();
        }

        /// <summary>
        /// 删除联系人相关属性
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task DeletePersonAsync(EntityDto input)
        {
            var entity = await _personRepository.GetAsync(input.Id);
            if (entity == null)
            {
                throw new UserFriendlyException("该联系人已经消失在数据库中，无法二次删除");
            }

            await _personRepository.DeleteAsync(input.Id);
        }

        #endregion 操作

        #region 私有方法

        protected async Task UpdatePersonAsync(PersonEditDto input)
        {
            var entity = await _personRepository.GetAsync(input.Id.Value);

            var person = new Person();
            person.EmailAddress = input.EmailAddress;
            person.Address = input.Address;

            await _personRepository.UpdateAsync(input.MapTo(entity));
        }

        protected async Task CreatePersonAsync(PersonEditDto input)
        {
            _personRepository.Insert(input.MapTo<Person>());
        }

        #endregion 私有方法
    }
}